package com.crazyhat.crazyhat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazyhat.crazyhat.dto.PostDTO;
import com.crazyhat.crazyhat.entity.Post;
import com.crazyhat.crazyhat.pojo.PostPublishWithFile;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.service.IPostService;
import com.crazyhat.crazyhat.vaildation.ValidationGroup.InsertValidAtionGroup;
import com.crazyhat.crazyhat.vaildation.ValidationGroup.UpdateValidAtionGroup;




/**
 * <p>
 * 动态 前端控制器
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private IPostService postService;


    // /**
    //  * 获取动态列表，分页
    //  * @param clubId
    //  * @param pageNum
    //  * @param pageSize
    //  * @return
    //  */
    // @GetMapping("/postList")
    // public Result<Page<Post>> getPostList(
    //         @RequestParam(value = "clubId") Integer clubId,
    //         @RequestParam(value = "pageNum") Integer pageNum,
    //         @RequestParam(value = "pageSize") Integer pageSize) {

    //     Page<Post> postList=postService.getPostList(clubId,pageNum,pageSize);

    //     if (postList!=null) {
    //         return Result.success(postList);
    //     } else {
    //         return Result.error("获取动态列表失败");
    //     }

    // }

    /**
     * 获取动态详情
     * @param postId
     * @return
     */
    @GetMapping("/postInfo")
    public Result<PostDTO> getPostInfo(@RequestParam(value = "postId") Integer postId) {
        
        PostDTO post=postService.getPostInfo(postId);

        if (post!=null) {
            return Result.success(post);
        } else {
            return Result.error("获取动态详情失败");
        }

    }
    

    /**
     * 发布动态，支持多文件上传
     * @param post
     * @return
     */
    @PostMapping(value = "/createPost",consumes = "multipart/form-data")
    public Result<Post> createPost(@ModelAttribute @Validated(InsertValidAtionGroup.class) PostPublishWithFile post) {
        
        Post newPost=postService.createPost(post);

        if (newPost!=null) {
            return Result.success(newPost);
        } else {
            return Result.error("发布动态失败");
        }
    }
    

    /**
     * 删除动态
     * @return
     */
    @DeleteMapping("/deletePost")
    public Result deletePost(@RequestParam(value = "postId") Integer postId) {

        boolean isSuccess=postService.deletePost(postId);

        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("删除动态失败");
        }
    }

    /**
     * 更新、修改动态
     * @param post
     * @return
     */
    @PostMapping("/updatePost")
    public Result<Post> updatePost(@ModelAttribute @Validated(UpdateValidAtionGroup.class) PostPublishWithFile post) {

        Post updatedPost=postService.updatePost(post);

        if (updatedPost!=null) {
            return Result.success(updatedPost);
        } else {
            return Result.error("更新动态失败");
        }
    }

    /**
     * 获取动态列表，分页，包含发布动态的用户信息
     * @param clubId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/postList")
    public Result<Page<PostDTO>> getPostPlusList(
            @RequestParam(value = "clubId") Integer clubId,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize) {
        
        Page<PostDTO> postList=postService.getPostPlusList(clubId,pageNum,pageSize);

        if (postList!=null) {
            return Result.success(postList);
        } else {
            return Result.error("获取动态列表失败");
        }
    }
}
